﻿Imports System.Data.OleDb

Public Class editProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("TutorsConnectionString").ConnectionString)
        Dim SqlString As String = "SELECT * FROM tblProfile WHERE TutorUserName=@f1"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text

        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        oleDbConn.Open()
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        If dr.HasRows() Then
            dr.Read()
            If Not IsDBNull(dr("Title")) Then ddl_title.SelectedValue = dr("Title")
            If Not IsDBNull(dr("FirstName")) Then tb_FirstName.Text = dr("FirstName")
            If Not IsDBNull(dr("LastName")) Then tb_LastName.Text = dr("LastName")
        End If
    End Sub

    Protected Sub btn_SaveProfile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_SaveProfile.Click
        Dim myGUID As New System.Guid
        myGUID = Guid.NewGuid()
        Dim newFileName = myGUID.ToString() + ".jpg"
        If fup_Picture.HasFile Then
            Dim picLocationOnServerHardDisk = Request.MapPath("Pictures") & "/" & newFileName
            fup_Picture.SaveAs(picLocationOnServerHardDisk)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("TutorsConnectionString").ConnectionString)
        Dim SqlString As String = "UPDATE tblProfile SET Title=@f1,FirstName=@f2,LastName@f3,Picture=@f4,PostCode=@f5,Subject=@f6,HourlyRate=@f7,WillingToTravel=@f8,Qualifications=@f9,Experience=@f10,Availability=@f11,EmailAddress=@f12 WHERE TutorUserName=@f13"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text

        cmd.Parameters.AddWithValue("@f1", ddl_title.SelectedValue)
        cmd.Parameters.AddWithValue("@f2", tb_FirstName.Text)
        cmd.Parameters.AddWithValue("@f3", tb_LastName.Text)
        cmd.Parameters.AddWithValue("@f4", "Pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f5", tb_PostCode.Text)
        cmd.Parameters.AddWithValue("@f6", tb_Subject.Text)
        cmd.Parameters.AddWithValue("@f7", tb_HourlyRate.Text)
        cmd.Parameters.AddWithValue("@f8", rbl_WillingToTravel.SelectedValue)
        cmd.Parameters.AddWithValue("@f9", tb_Qualifications.Text)
        cmd.Parameters.AddWithValue("@f10", tb_Experience.Text)
        cmd.Parameters.AddWithValue("@f11", rbl_Availability.SelectedValue)
        cmd.Parameters.AddWithValue("@f12", tbEmailAddress.Text)
        cmd.Parameters.AddWithValue("@f13", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("Profile.aspx")

    End Sub
End Class